package com.maomao.admin.service.system.impl;

import com.maomao.admin.common.utils.StringUtils;
import com.maomao.admin.domain.core.vo.TableDataInfo;
import com.maomao.admin.domain.system.entry.QSysOpLog;
import com.maomao.admin.domain.system.entry.SysOpLog;
import com.maomao.admin.persistence.repository.SysOpLogRepository;
import com.maomao.admin.service.system.AbstractPageService;
import com.maomao.admin.service.system.ISysOpLogService;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 操作日志 服务层处理
 *
 * @author mao.kaiyin
 */
@Service
public class SysOpLogServiceImpl extends AbstractPageService implements ISysOpLogService {


    @Resource
    private JPAQueryFactory queryFactory;

    @Autowired
    private SysOpLogRepository logRepository;

    /**
     * 新增操作日志
     *
     * @param opLog 操作日志对象
     */
    @Override
    public void insertOplog(SysOpLog opLog) {
        logRepository.save(opLog);
    }

    /**
     * 查询系统操作日志集合
     *
     * @param opLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOpLog> selectOpLogList(SysOpLog opLog) {
        JPAQuery<SysOpLog> jpaQuery = this.getLogJPAQuery(opLog);
        return jpaQuery.fetch();
    }


    JPAQuery<SysOpLog> getLogJPAQuery(SysOpLog opLog) {
        JPAQuery<SysOpLog> jpaQuery = queryFactory.selectFrom(QSysOpLog.sysOpLog);
        List<Predicate> predicates = new LinkedList<>();
        if (StringUtils.isNotBlank(opLog.getTitle())) {
            predicates.add(QSysOpLog.sysOpLog.title.contains(opLog.getTitle()));
        }
        if (opLog.getBusinessType() != null) {
            predicates.add(QSysOpLog.sysOpLog.businessType.eq(opLog.getBusinessType()));
        }
        if (opLog.getBusinessTypes() != null && opLog.getBusinessTypes().length > 0) {
            predicates.add(QSysOpLog.sysOpLog.businessType.in(opLog.getBusinessTypes()));
        }
        if (opLog.getStatus() != null) {
            predicates.add(QSysOpLog.sysOpLog.status.eq(opLog.getStatus()));
        }
        if (StringUtils.isNotBlank(opLog.getOpName())) {
            predicates.add(QSysOpLog.sysOpLog.opName.contains(opLog.getOpName()));
        }
        Map<String, Object> params = opLog.getParams();
        if (params != null) {
            Object beginTime = params.get("beginTime");
            if (beginTime instanceof Date) {
                predicates.add(QSysOpLog.sysOpLog.opTime.gt((Date) beginTime));
            }

            Object endTime = params.get("endTime");
            if (endTime instanceof Date) {
                predicates.add(QSysOpLog.sysOpLog.opTime.lt((Date) endTime));
            }
        }
        if (predicates.size() > 0) {
            jpaQuery.where(predicates.toArray(new Predicate[0]));
        }
        jpaQuery.orderBy(QSysOpLog.sysOpLog.opId.desc());
        return jpaQuery;
    }

    /**
     * 查询系统操作日志集合
     *
     * @param opLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public TableDataInfo<SysOpLog> selectOpLogPage(SysOpLog opLog) {
        JPAQuery<SysOpLog> jpaQuery = this.getLogJPAQuery(opLog);
        super.startPage(jpaQuery);
        return new TableDataInfo<>(jpaQuery.fetch(), jpaQuery.fetchCount());
    }


    /**
     * 批量删除系统操作日志
     *
     * @param opIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOpLogByIds(Long[] opIds) {
        logRepository.deleteAllByIdInBatch(Arrays.asList(opIds));
        return 1;
    }

    /**
     * 查询操作日志详细
     *
     * @param opId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOpLog selectOpLogById(Long opId) {
        return logRepository.findById(opId).get();
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOpLog() {
        logRepository.deleteAll();
    }
}
